package com.anmong.parser.controller;

import com.anmong.parser.model.MergeRequest;
import com.anmong.parser.service.SubtitleParserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class SubtitleParserController {

    @Autowired
    SubtitleParserService subtitleParserService;

    @PostMapping("/merge")
    public ResponseEntity<String> mergeSubtitles(@RequestBody MergeRequest request) {
        try {
            if (request.getNovelText() == null || request.getNovelText().isBlank() ||
                    request.getSrtText() == null || request.getSrtText().isBlank()) {
                return ResponseEntity.badRequest().body("小说原文和字幕内容都不能为空！");
            }

            String result = subtitleParserService.merge(request.getNovelText(), request.getSrtText());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError().body("服务器处理时发生内部错误: " + e.getMessage());
        }
    }
}
